<template>
  <div class="departments-container">
    <el-card>
      <!-- <el-row type="flex" justify="space-around" align="middle" style="height:60px">
        <el-col>
          <span>江苏传智播客教育科技股份有限公司</span>
        </el-col>
        <el-col :span="4">
          <el-row type="flex" justify="end" align="middle" style="height:">
            <el-col>
              <span>负责人</span>
            </el-col>
            <el-col>
              <el-dropdown>
                <span>操作<i class="el-icon-arrow-down el-icon--right" /></span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item>添加子部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-col>
      </el-row> -->
      <treeTools :tree-node="company" :is-root="false" @add="handleAdd" />
    </el-card>
    <el-tree v-loading="loading" :data="departs" :default-expand-all="true" :props="defaultProps">
      <!-- 会根据data接收到的数据 循环 有多少数据 就有多少节点
      data: 当前节点的 数据-->
      <!-- <span slot-scope="{data}">asdasdasd{{ data }}</span> -->
      <!-- <el-row slot-scope="{data}" type="flex" justify="space-around" align="middle" style="height:60px; width:100%">
        <el-col>
          <span>{{ data.name }}</span>
        </el-col>
        <el-col :span="4">
          <el-row type="flex" justify="end" align="middle" style="height:">
            <el-col>
              <span>{{ data.manager }}</span>
            </el-col>
            <el-col>
              <el-dropdown>
                <span>操作<i class="el-icon-arrow-down el-icon--right" /></span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item>添加子部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-col>
      </el-row> -->
      <treeTools slot-scope="{data}" :tree-node="data" @add="handleAdd" @edit="handleEdit" @refreshList="getDepartments" />
    </el-tree>

    <addDept ref="addDept" :dialog-visible.sync="dialogVisible" :current-node="currentNode" />
    <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import addDept from './components/add-dept.vue'
import { getDepartmentsApi } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'DepartmentsIndex',
  components: {
    treeTools,
    addDept
  },
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      departs: [],
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      dialogVisible: false,
      currentNode: {},
      loading: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      try {
        this.loading = true
        const res = await getDepartmentsApi()
        // console.log(tranListToTreeData(res.depts, ''))
        // console.log(res)
        this.departs = tranListToTreeData(res.depts, '')
        this.company = { name: res.companyName, manager: res.companyManage, id: '' }
      } finally {
        this.loading = false
      }
    },
    handleAdd(node) {
      console.log('到达父组件', node)
      this.currentNode = node // 保存在 currentNode 父组件
      // currentNode 给 addDept
      this.dialogVisible = true
    },
    handleEdit(node) {
      this.dialogVisible = true
      // 数据会显
      this.$refs.addDept.formData = { ...node }
      // 加不加
      // currentNode 当前节点 为了保证数据的干净
      this.currentNode = node
    }
  }
}
</script>

<style lang="scss" scoped>
.departments-container{
  width: 900px;
  margin: 20px auto;
   .el-tree::v-deep  {
    .el-tree-node__content {
      padding-right: 20px;
    }
  }
}
</style>

